package com.example.xiaomaotv;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


public class ModifyActivity extends AppCompatActivity {

    private EditText etOldPassword, etNewPassword, etConfirmPassword;
    private SharedPreferences sp;
    private MySQL helper;
    private SQLiteDatabase database;
    private String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);

        // 初始化控件
        initViews();
        // 初始化数据库
        initDatabase();
        // 获取当前账号
        initSharedPreferences();
    }

    private void initViews() {
        etOldPassword = findViewById(R.id.et_old_password);
        etNewPassword = findViewById(R.id.et_new_password);
        etConfirmPassword = findViewById(R.id.modify_new_password);
    }
    private void initDatabase() {
        helper = new MySQL(ModifyActivity.this, "XiaoMaoTv", null, 1);
        database = helper.getWritableDatabase();
    }
    private void initSharedPreferences() {
        sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        account = sp.getString("account", "");
    }
    public void BT_Modify_onClick(View view){
        //获取用户输入密码
        String oldPasswordInput = etOldPassword.getText().toString();
        String newPasswordInput = etNewPassword.getText().toString();
        String confirmPasswordInput = etConfirmPassword.getText().toString();
        Cursor cursor = database.query("user", null, "account=?", new String[]{account}, null, null, null);
        // 检查Cursor是否有数据
        if (cursor == null || !cursor.moveToFirst()) {
            Toast.makeText(this, "用户不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        String password = cursor.getString(cursor.getColumnIndex("password"));
        if (oldPasswordInput.isEmpty() || newPasswordInput.isEmpty() || confirmPasswordInput.isEmpty()) {
            Toast.makeText(this, "所有字段不能为空", Toast.LENGTH_SHORT).show();
        } else if (newPasswordInput.length() < 8) {
            Toast.makeText(this, "新密码长度不能少于8位", Toast.LENGTH_SHORT).show();
        } else if (!newPasswordInput.equals(confirmPasswordInput)) {
            Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
        } else if (!oldPasswordInput.equals(password)) {
            Toast.makeText(this, "当前密码错误", Toast.LENGTH_SHORT).show();
        } else if (newPasswordInput.equals(oldPasswordInput)) {
            Toast.makeText(this, "新密码与旧密码不能保持一致", Toast.LENGTH_SHORT).show();
        } else {
            ContentValues values = new ContentValues();
            values.put("password", newPasswordInput);
            int rowsAffected = database.update("user", values, "account = ?", new String[]{account});
            if (rowsAffected > 0) {
                Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "修改失败，请重试", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void Bt_Cancel_Modify(View view){
        finish();
    }
}
